# -*- coding: utf-8 -*-
# @Date    : 2024/1/22
# @Author  : zouy
# @Email: 874112501@qq.com
import re

import requests
import execjs
from loguru import logger

ss = requests.Session()
ss.headers = {
    "Host": "xueqiu.com",
    "Connection": "keep-alive",
    "Cache-Control": "max-age=0",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "Referer": "https://xueqiu.com/today",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9"
}

# 1. 请求首页,得到生成cooke  acw_sc__v2 的js代码,重点是动态变化的参数:arg1
url = 'https://xueqiu.com/today'

resp = ss.get(url)
data = resp.text
print(ss.cookies.get_dict())  # {'acw_tc': '2760826317061732287132702ed8644e680f57ccb571293f0805451e1e0e30'}

arg1 = re.findall(r"var\s*arg1\s*=\s*'(.*?)'", data)[0]
jscode = r'''
var arg1 = '{arg1}';
var unsbox = function () { var _0x4b082b = [ 0xf, 0x23, 0x1d, 0x18, 0x21, 0x10, 0x1, 0x26, 0xa, 0x9, 0x13, 0x1f, 0x28, 0x1b, 0x16, 0x17, 0x19, 0xd, 0x6, 0xb, 0x27, 0x12, 0x14, 0x8, 0xe, 0x15, 0x20, 0x1a, 0x2, 0x1e, 0x7, 0x4, 0x11, 0x5, 0x3, 0x1c, 0x22, 0x25, 0xc, 0x24 ]; var _0x4da0dc = []; var _0x12605e = ''; for (var _0x20a7bf = 0x0; _0x20a7bf < this["length"]; _0x20a7bf++) { var _0x385ee3 = this[_0x20a7bf]; for (var _0x217721 = 0x0; _0x217721 < _0x4b082b['length']; _0x217721++) { if (_0x4b082b[_0x217721] == _0x20a7bf + 0x1) { _0x4da0dc[_0x217721] = _0x385ee3; } } } _0x12605e = _0x4da0dc['\x6a\x6f\x69\x6e'](''); return _0x12605e; }

function hexXor(_0x4e08d8) { var _0x5a5d3b = ''; for (var _0xe89588 = 0x0; _0xe89588 < this.length && _0xe89588 <  this.length; _0xe89588 += 0x2) { var _0x401af1 = parseInt(this['slice'](_0xe89588, _0xe89588 + 0x2), 0x10); var _0x105f59 = parseInt(_0x4e08d8['slice'](_0xe89588, _0xe89588 + 0x2), 0x10); var _0x189e2c = (_0x401af1 ^ _0x105f59)['toString'](0x10); if (_0x189e2c['length'] == 0x1) { _0x189e2c = '\x30' + _0x189e2c; } _0x5a5d3b += _0x189e2c; } return _0x5a5d3b; }

function getCookie() {
    var _0x5e8b26 = "3000176000856006061501533003690027800375" 
    var _0x23a392 = unsbox.call(arg1)
    var args2 = hexXor.call(_0x23a392,_0x5e8b26)
    return args2
}
'''.replace('{arg1}', arg1)

acw_sc__v2 = execjs.compile(jscode).call('getCookie')
logger.info('1. 请求首页js生成cookie acw_sc__v2: ' + acw_sc__v2)

# 2.带上 acw_sc__v2 再次请求首页，返回cookie  xq_a_token
ss.cookies['acw_sc__v2'] = acw_sc__v2
res = ss.get(url)
logger.info('2. 再次请求首页,服务器返回cookie: ' + str(res.cookies.get_dict()))

# 3. 带上上次请求生成的xq_a_token，便能正常请求得到数据
ss.headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Pragma": "no-cache",
    "Referer": "https://xueqiu.com/",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
    # "elastic-apm-traceparent": "00-1d680a9b965d254a7565176875f6fa4d-37238e803b0fc1c2-01",
}
url = "https://xueqiu.com/statuses/hot/listV2.json"
params = {
    "since_id": "-1",
    "max_id": "588250",
    "size": "15"
}
response = ss.get(url, params=params)

print(response.json()["items"][0])
logger.info(f'请求数据接口: {response}')
